//
//  ALPSortedViewController.m
//  AlpFM
//
//  Created by KING on 16/1/25.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPSortedViewController.h"
#import "Cell.h"
#import "ALPHead1View.h"
#import "ALPAppTools.h"
#import "ALPSortedDetailViewController.h"
@interface ALPSortedViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain)UICollectionView *mycollectionView;
@property (nonatomic, retain)NSMutableArray *allArray;
@property (nonatomic, retain)NSArray *array;
@property (nonatomic, retain)NSArray *arrayClass1;
@property (nonatomic, retain)NSArray *arrayClass2;
@property (nonatomic, retain)NSArray *arrayClass3;
@property (nonatomic, retain)NSArray *arrayClass4;
//@property (nonatomic, retain)ALPClassModel *classModel;

@end

@implementation ALPSortedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(375 / 2, 50);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.flowLayout.minimumLineSpacing = 1;
    self.flowLayout.minimumInteritemSpacing = 1;
    //    self.flowLayout.headerReferenceSize = CGSizeMake(375, 200);
    //    self.flowLayout.footerReferenceSize = CGSizeMake(0, 0);
    self.mycollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375, 513)collectionViewLayout:self.flowLayout];
    self.mycollectionView.delegate = self;
    self.mycollectionView.dataSource = self;
    self.mycollectionView.bounces = YES;
    [self.mycollectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"cell"];
    [self.mycollectionView registerClass:[ALPHead1View class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.mycollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
    self.mycollectionView.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1];
    [self.view addSubview:self.mycollectionView];
    [self loadData];
}
- (void)loadData{
    
    [ALPAppTools creatingDataTask:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=11&scale=2" parameters:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic1 in array) {
            ALPClassModel *classModel = [[ALPClassModel alloc]init];
            [classModel setValuesForKeysWithDictionary:dic1];
            [self.allArray addObject:classModel];
        }
        self.array = [NSMutableArray array];
        self.array = [self.allArray subarrayWithRange:NSMakeRange(0, 5)];
        [self.mycollectionView reloadData];
        self.arrayClass1 = [NSArray array];
        self.arrayClass1 = [self.allArray subarrayWithRange:NSMakeRange(5, 6)];
        self.arrayClass2 = [NSArray array];
        self.arrayClass2 = [self.allArray subarrayWithRange:NSMakeRange(11, 6)];
        self.arrayClass3 = [NSArray array];
        self.arrayClass3 = [self.allArray subarrayWithRange:NSMakeRange(17, 6)];
        self.arrayClass4 = [NSArray array];
        self.arrayClass4 = [self.allArray subarrayWithRange:NSMakeRange(19, 2)];
    }];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.arrayClass1.count;
    }else if (section == 1){
        return self.arrayClass2.count;
    }else if (section == 2){
        return self.arrayClass3.count;
    }else{
        return self.arrayClass4.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.section == 0) {
        cell.classModel = self.arrayClass1[indexPath.item];
    }else if (indexPath.section == 1){
        cell.classModel = self.arrayClass2[indexPath.item];
    }else if (indexPath.section == 2){
        cell.classModel = self.arrayClass3[indexPath.item];
    }else{
        cell.classModel = self.arrayClass4[indexPath.item];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ALPSortedDetailViewController *sortedDetailVC = [[ALPSortedDetailViewController alloc]init];
    if (indexPath.section == 0) {
        ALPClassModel *classModel = self.arrayClass1[indexPath.item];
        [self.navigationController pushViewController:sortedDetailVC animated:YES];

        sortedDetailVC.myId = classModel.myId;
        
    }else if (indexPath.section == 1){
        ALPClassModel *classModel = self.arrayClass2[indexPath.item];
        [self.navigationController pushViewController:sortedDetailVC animated:YES];

        sortedDetailVC.myId = classModel.myId;

    }else if (indexPath.section == 2){
        ALPClassModel *classModel = self.arrayClass3[indexPath.item];
        [self.navigationController pushViewController:sortedDetailVC animated:YES];

        sortedDetailVC.myId = classModel.myId;

    }else{
        ALPClassModel *classModel = self.arrayClass4[indexPath.item];
        [self.navigationController pushViewController:sortedDetailVC animated:YES];

        sortedDetailVC.myId = classModel.myId;

    }

    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ALPHead1View *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        headView.array = self.array;
        
        return headView;
    }
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1" forIndexPath:indexPath];
    return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(375, 200);
    }
    return CGSizeMake(375, 10);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
